package htmlPicReader;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import aut.commons.threadPool.ThreadPool;

public class ReadBatchHtmlPic {
	
	private static Logger log = Logger.getLogger("downtv");
	
	private static final String TVHOST = "epg.tvsou.com";
	
	private static final String DATASAVEPATH = "data/";
	
	private static final String UNKNOWNPICNUMNM = "unKnownPicNum.dat";
	
	private static final String PICPATH = "/imagesnum/";
	
	private static final String PICEXTNM = ".gif";
	
	private static final String PICSAVEPATH = "pic/";
	
	private String channelDirNm = null;
	
	public ReadBatchHtmlPic(String channelDirNm) {
		this.channelDirNm = channelDirNm;
	}

	public void batchReadHtml() throws IOException{
		
		InetAddress address = InetAddress.getByName(TVHOST);
		SocketAddress socketAddress = new InetSocketAddress(address, 80);		
		StringBuffer unKnPicNumPath = new StringBuffer(DATASAVEPATH + channelDirNm
				+ UNKNOWNPICNUMNM);
		File file = new File(unKnPicNumPath.toString());
		URL url = file.toURL();
		Properties pu = new Properties();
		pu.load(url.openStream());
		
		log.info("Start time: " + new Date());
		ArrayList workers = new ArrayList();
		Iterator it = pu.entrySet().iterator();
		while(it.hasNext()){
			Entry entry = (Entry)it.next();				
			String tvPath =  entry.getKey().toString();
			ReadHtmlPic readHtmlPic = new ReadHtmlPic(TVHOST, socketAddress);
			String tvPathTmp = new StringBuffer(PICPATH + tvPath +PICEXTNM).toString();
			String saveFNPreTmp = new StringBuffer(tvPath +PICEXTNM).toString();
			workers.add(new Worker(readHtmlPic, tvPathTmp, saveFNPreTmp));
		}
		
		ThreadPool threadPool = new ThreadPool(20);
		for(int i = 0; i < workers.size(); i++){
			threadPool.execute((Worker)workers.get(i));
		}
		for(int i = 0; i < workers.size(); i++){
			Worker item = (Worker)workers.get(i);
			synchronized(item){
				while(!item.isOver()){
					try {
						item.wait();
					} catch (InterruptedException e) {
						log.error(e, e);
						e.printStackTrace();
					}
				}
			}
		}
		threadPool.overPool();
		log.info("End time: " + new Date());
	}
	
	private final void initPicSaveDir(){
		File picDir = new File(PICSAVEPATH);
		if(!picDir.exists()){
			picDir.mkdir();
		}
	}
	
	public void work() throws IOException{	
		initPicSaveDir();
		batchReadHtml();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {		
		try {
			ReadBatchHtmlPic readBatchHtmlPic = new ReadBatchHtmlPic(null);
			readBatchHtmlPic.work();
		} catch (IOException e) {
			log.error(e, e);
			e.printStackTrace();
		}
	}

	class Worker implements Runnable{
		
		private boolean overFlag = false;
		private ReadHtmlPic readHtmlPic = null;
		private String tvPath = null;
		private String saveFNPre = null;		
		
		public Worker(ReadHtmlPic readHtmlPic, String tvPath, String saveFNPre) {
			this.readHtmlPic = readHtmlPic;
			this.tvPath = tvPath;
			this.saveFNPre = saveFNPre;			
		}

		public void run() {	
			/*for(int i = 1; i <= 7; i++){
				StringBuffer tvPathTmp = new StringBuffer(tvPath + "W" + i + ".htm");
				StringBuffer saveFNPreTmp = new StringBuffer(saveFNPre + "_" + week[i - 1] + ".html");
				readHtml.readBinHtml(tvPathTmp.toString(), saveFNPreTmp.toString());
			}*/
			readHtmlPic.readHtmlPic(tvPath, saveFNPre);			
			synchronized(this){
				overFlag = true;
				this.notify();
			}
		}
		
		public boolean isOver(){
			return overFlag;
		}
	}
}